import { createVNode, render } from 'vue'
import Message from '@/components/message/index.vue'

const divNode = createVNode('div')
render(divNode, document.body)
const container = divNode.el
type message = { title: string; text: string }
const useMessage = ({ title, text }: message): Promise<string> => {
  return new Promise((resolve, reject) => {
    const confirmCallback = () => {
      render(null, container as Element)
      resolve('点击确认')
    }
    const cancelCallback = () => {
      render(null, container as Element)
      reject(new Error('点击取消'))
    }
    const VNode = createVNode(Message, {
      title,
      text,
      confirmCallback,
      cancelCallback
    })
    render(VNode, container as Element)
  })
}

export default useMessage
